<?php

namespace Biz\service\advertisement\apiModule;


use Doraemon\tools\BlockType;
use Doraemon\tools\ImgUrl;
use Doraemon\tools\HhzClient;

class Tab2DesignerBanner extends BaseModule
{
    const FORMAT_HIT = 'formatPosHit';

    public function format($hit, $uid, $userProfileExt, $statSignExt)
    {
        $response = empty($hit) ? [] : array_map(function ($value) {
            $id = intval($value['id']);
            $index = intval($value['pos']);
            $statSign = $this->statSign($value);
            //5.21.0新埋点， 并去除冗余变量
            $ret = [
                'type' => BlockType::TYPE_BANNER,
                'banner' => [
                    'id' => $id,
                    'index' => $index,
                    'banner' => empty($value['pic_id']) ? '' : ImgUrl::getUrl(ImgUrl::APPTYPE_OP, $value['pic_id'], ImgUrl::IMAGE_SIZE_OP_ORI),
                    'link' => $this->generateLink($value['link_type'], $value['link_params']),
                ],
                'index' => $index,
            ];
            if (version_compare(HhzClient::getClientVersion(), '5.21.0', '>=')) {
                $ret['statSign'] = $statSign;
            } else {
                $ret['banner']['statSign'] = $statSign;
            }
            return $ret;

        }, $hit);

        if (!empty($response)) {
            $index = array_column($response, 'index');
            array_multisort($index, SORT_ASC, $response);
        }

        return $response;
    }

    public function uniquePos($v): string
    {
        return $v['pos'];
    }
}